package com.data.center.service.mongo;
import com.data.center.entity.mongo.MongoStaff;
import com.data.center.mapper.mongo.MongoStaffRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;
import java.util.Optional;
@Service
public class MongoStaffService {


        @Autowired
        private MongoStaffRepository staffRepository;

        // 查询所有员工
        public List<MongoStaff> getAllStaff() {
            return staffRepository.findAll();
        }

        // 根据 staffId 查询员工
        public Optional<MongoStaff> getStaffById(String staffId) {
            return staffRepository.findByStaffId(staffId);
        }

        // 添加新员工
        public MongoStaff saveStaff(MongoStaff staff) {
            return staffRepository.save(staff);
        }

        // 更新员工信息
        public MongoStaff updateStaff(String staffId, MongoStaff updatedStaff) {
            Optional<MongoStaff> existingStaff = staffRepository.findByStaffId(staffId);
            if (existingStaff.isPresent()) {
                updatedStaff.setStaffId(staffId); // 确保 ID 不变
                return staffRepository.save(updatedStaff);
            }
            return null; // 或者抛出异常
        }

        // 删除员工
        @Autowired
        private MongoTemplate mongoTemplate;

    public void deleteByStaffId(String staffId) {
        Query query = new Query(Criteria.where("staffId").is(staffId));
        mongoTemplate.remove(query, MongoStaff.class);
    }

}
